package org.dromara.quickswing.ui.app;

import cn.hutool.core.util.StrUtil;

import java.awt.*;
import java.io.File;
import java.util.prefs.Preferences;

import static org.dromara.quickswing.constant.QSConstant.*;

/**
 * @author Jin
 */
public abstract class QSPrefs {
    private final Preferences state;


    protected abstract String getKeyPrefix();

    public Preferences getState() {
        return state;
    }

    protected QSPrefs(String rootPath) {
        state = Preferences.userRoot().node(rootPath);
        this.initDefaults();
    }

    public Point getPoint() {
        int x = state.getInt(getKeyPrefix() + "_" + KEY_LOCATION_X, 0);
        int y = state.getInt(getKeyPrefix() + "_" + KEY_LOCATION_Y, 0);
        return new Point(x, y);
    }

    public Dimension getSize() {
        int width = state.getInt(getKeyPrefix() + "_" + KEY_APP_WIDTH, 0);
        int height = state.getInt(getKeyPrefix() + "_" + KEY_APP_HEIGHT, 0);
        return new Dimension(width, height);
    }

    public String getAppTheme() {
        return state.get(getKeyPrefix() + "_" + KEY_APP_THEME, DEFAULT_APP_THEME_DARK);
    }

    public String getDataPath() {
        return state.get(getKeyPrefix() + "_" + KEY_APP_DATA_PATH, DEFAULT_APP_DATA_PATH);
    }

    public void setDataPath(String path) {
        if (StrUtil.isEmpty(path)) {
            return;
        }
        state.put(getKeyPrefix() + "_" + KEY_APP_DATA_PATH, path);
    }

    public String getLanguage() {
        return state.get(getKeyPrefix() + "_" + KEY_APP_LANGUAGE, DEFAULT_APP_LANGUAGE_CN);
    }

    public String getResourceBundlePath() {
        return state.get(getKeyPrefix() + "_" + KEY_APP_RESOURCE_BUNDLE_PATH, DEFAULT_KEY_APP_RESOURCE_BUNDLE);
    }

    public void setResourceBundlePath(String path) {
        if (StrUtil.isEmpty(path)) {
            return;
        }
        state.put(getKeyPrefix() + "_" + KEY_APP_RESOURCE_BUNDLE_PATH, path);
    }

    public void setLocation(Point location) {
        if (location == null) {
            return;
        }
        state.putInt(getKeyPrefix() + "_" + KEY_LOCATION_X, location.x);
        state.putInt(getKeyPrefix() + "_" + KEY_LOCATION_X, location.y);
    }

    public void setSize(Dimension size) {
        if (size == null) {
            return;
        }
        state.getInt(getKeyPrefix() + "_" + KEY_APP_WIDTH, size.width);
        state.getInt(getKeyPrefix() + "_" + KEY_APP_HEIGHT, size.height);
    }

    public void setAppTheme(String appTheme) {
        if (StrUtil.isEmpty(appTheme)) {
            return;
        }
        state.put(getKeyPrefix() + "_" + KEY_APP_THEME, appTheme);
    }

    public void setLanguage(String language) {
        if (StrUtil.isEmpty(language)) {
            return;
        }
        state.put(getKeyPrefix() + "_" + KEY_APP_LANGUAGE, language);
    }

    public void setLogbackFile(String logbackFile) {
        if (StrUtil.isEmpty(logbackFile)) {
            return;
        }
        state.put(getKeyPrefix() + "_" + KEY_LOG_FILE, logbackFile);
    }

    public String getLogbackFile() {
        return state.get(getKeyPrefix() + "_" + KEY_LOG_FILE, DEFAULT_APP_DATA_PATH + File.separator + DEFAULT_LOG_FILE);
    }

    public void setAppName(String appName) {
        if (StrUtil.isEmpty(appName)) {
            return;
        }
        state.put(getKeyPrefix() + "_" + KEY_APP_NAME, appName);
    }

    public String getAppName() {
        return state.get(getKeyPrefix() + "_" + KEY_APP_NAME, DEFAULT_APP_NAME);
    }


    public void setDbFileName(String dbFileName) {
        if (StrUtil.isEmpty(dbFileName)) {
            return;
        }
        state.put(getKeyPrefix() + "_" + KEY_DB_FILE_NAME, dbFileName);
    }

    public String getDbFileName() {
        return state.get(getKeyPrefix() + "_" + KEY_DB_FILE_NAME, DEFAULT_DB_FILE_NAME);
    }

    protected abstract void initDefaults();

}